package com.ruoyi.web.controller.app;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.domain.TourQuestion;
import com.ruoyi.system.domain.TourVideo;
import com.ruoyi.system.service.ISysNoticeService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ITourQuestionService;
import com.ruoyi.system.service.ITourVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/app")
public class Universal extends BaseController {
    @Autowired
    private ITourQuestionService tourQuestionService;
    @Autowired
    private SysLoginService loginService;
    @Autowired
    private ISysNoticeService noticeService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ITourVideoService tourVideoService;

    /**
     * 查询vlog管理列表
     */
    @GetMapping("/videolist")
    public TableDataInfo list(TourVideo tourVideo) {
        startPage();
        List<TourVideo> list = tourVideoService.selectTourVideoList(tourVideo);
        return getDataTable(list);
    }

    /**
     * 获取通知公告列表
     */
    @GetMapping("/notices")
    public TableDataInfo list(SysNotice notice) {
        startPage();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    @PostMapping("/register")
    public AjaxResult add(@Validated @RequestBody SysUser user) {
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName()))) {
            return AjaxResult.error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
            return AjaxResult.error("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
            return AjaxResult.error("新增用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
//        user.setCreateBy(SecurityUtils.getUsername());
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        return toAjax(userService.insertUser(user));
    }

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.app_login(loginBody.getUsername(), loginBody.getPassword());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @GetMapping("/questions")
    public TableDataInfo list(TourQuestion tourQuestion) {
        startPage();
        List<TourQuestion> list = tourQuestionService.selectTourQuestionList(tourQuestion);
        return getDataTable(list);
    }
}
